<?php
namespace app\shop\library\order\item;

use app\shop\controller\admin\Member;
use app\shop\library\order\OrderItem;
use app\shop\library\order\Order;
use cubox\Cubox;
use app\shop\library\coupon\MemberCoupon;

class goodsCoupon extends OrderItem{

    public $memberCouponId;
    public $usedGoods = [];
    public function count(Order &$order){
        if (!isset($this->post['goodsCoupon']) ||  $this->post['goodsCoupon']=='') return;

        $this->memberCouponId =  $this->post['goodsCoupon'];
        if ($this->memberCouponId){
            $goodsData = $order->list();
            $totalDiscount = 0;
             
            $couponUserData =  MemberCoupon::get($this->memberCouponId);
            
            if (!$couponUserData){
                throw new \Exception('优惠券不正确 02');
            }

            $coupon =   $couponUserData['coupon'];

            $couponGoodsList = $this->MatchGoodsList($goodsData,$coupon);
            $goodsPrice  = 0;

            foreach ($couponGoodsList as $goodsItem){
                
                //计算原总价还是优惠后的总价看策略
                // $goodsPrice += $goodsItem['total'];
                $goodsPrice += $goodsItem['pay'];
                 
                $this->usedGoods[] = $goodsItem['goods_id']; 
            }

            if ($goodsPrice < $coupon['condition_price']){
                throw new \Exception('优惠券不正确 03');
            }

            if ($goodsPrice <=0){
                throw new \Exception('优惠券不正确 04');
            }

            //计算优惠券优惠率
            $avgPrice = $coupon['price'] / $goodsPrice;

            foreach($couponGoodsList as $goodsItem){
                $order->setGoodsDiscount($goodsItem['goods_id'],$goodsItem['total']*$avgPrice);
            }

            $totalDiscount += $coupon['price'];
            
        }
        $this->save_lab = true;
        $order->addDetail('coupon',$totalDiscount*-1);
    }

    public function save(Order $order){
        if (!$this->save_lab) return;
        $app = Cubox::app('shop');
        
        $memberCoupon = $app->model('MemberCoupon');
        
        $update = [];
        $update['order_id'] = $order->order_id;
        $update['status'] = 'used';
        $update['use_at'] = date('Y-m-d H:i:s');
        $update['use_goods'] = implode(',',$this->usedGoods);
        $memberCoupon->where('member_id',$this->member_id)->where('member_coupon_id',$this->memberCouponId)->update($update);
        MemberCoupon::use($this->memberCouponId,$this->member_id);
    }

    public function rollback(Order $order){
        MemberCoupon::clearCache($this->member_id);
    }

    public  function MatchGoodsList($goodsList,$coupon):array{
        $matchGoodslist = [];
        foreach($goodsList as $goods){
            if ($coupon['apply_type']=='all'){
                $matchGoodslist[]= $goods;
            }else{
                if (in_array($goods['goods_id'],$coupon['apply_goods_list'])){
                    $matchGoodslist[]= $goods;
                }else{
                    if (in_array($goods['cate_id'],$coupon['apply_cate_list'])){
                        $matchGoodslist[]= $goods;
                    }
                }
            }
        }

        return $matchGoodslist;
    }
}